// 工作范围选定的数据
var buildWorkRangeArr = [];
$(function () {
  // $('.buildWorkRange-pop').addClass('is-visible3');
  // runderBuildWorkRangeList();
  // 弹出人员工作范围页面
  $('.buildWorkRange-trigger').on('click', function () {
    console.log(999)
    event.preventDefault();
    $('.buildWorkRange-pop').addClass('is-visible3');
    // buildWorkRangeArr = [];
    $('.buildWorkRangeItem ul').html('');
    runderBuildWorkRangeList();
  });
  // //工作范围管理  打开窗口
  // $('.buildWorkRange-trigger').on('click', function (event) {
  //     event.preventDefault();
  //     $('.buildWorkRange-pop').addClass('is-visible3');
  //     runderBuildWorkRangeList();

  // });
  var buildWorkRangeContent = [];
  buildWorkRangePageNum = '';
  // 左侧楼宇
  function runderBuildWorkRangeList() {
    $('.buildWorkRangeSearch').val('');
    // var buildWorkRangeSearchContent = $('.buildWorkRangeSearch').val();
    var obj = {};
    obj.size = 10000;
    obj.page = 0;
    // obj.address = buildWorkRangeSearchContent;
    var ret = getBuildingListInfo(obj);
    ret.then(function (data) {
      console.log(data);
      var content = data.data.content;
      buildWorkRangeContent = content;
      buildWorkRangePageNum = data.data.totalPages;
      size = data.data.size;
      $('#buildWorkRangeModal .tbodyList').html('');
      for (var i = 0; i < content.length; i++) {
        var flag = true;
        for (var j = 0; j < buildWorkRangeArr.length; j++) {
          // console.log(content[i].id, buildWorkRangeArr[j].id, content[i].id == buildWorkRangeArr[j].id)
          if (content[i].id == buildWorkRangeArr[j].id) {
            $('#buildWorkRangeModal .tbodyList').append('<tr class="build' + content[i].id + '"><td style="margin-right: 5px;width:6%;"><input type="checkBox" checked>' + (i + 1) + '</td><td width="21%">' + content[i].communityName + '</td><td width="21%">' + content[i].buildingName + '</td><td width="42%">' + content[i].address + '</td><td width="10%">' + content[i].org.orgName + '</td></tr>'); //<td><input type="checkbox" class="check" devid="' + content[i].id + '"></td>
            flag = false;
          }
        }
        if (flag) {
          $('#buildWorkRangeModal .tbodyList').append('<tr class="build' + content[i].id + '"><td style="margin-right: 5px;width:6%;"><input type="checkBox">' + (i + 1) + '</td><td width="21%">' + content[i].communityName + '</td><td width="21%">' + content[i].buildingName + '</td><td width="42%">' + content[i].address + '</td><td width="10%">' + content[i].org.orgName + '</td></tr>'); //<td><input type="checkbox" class="check" devid="' + content[i].id + '"></td>

        }
      }
      console.log(buildWorkRangeArr)
      $('.buildWorkRangeItem ul').html('');
      for (var j = 0; j < buildWorkRangeArr.length; j++) {
        $('.buildWorkRangeItem ul').append('<li>' + (j + 1) + '、' + buildWorkRangeArr[j].address + '</li>');
      }

      // buildWorkRangePageFunc(buildWorkRangePageNum, n);
    }).catch(function (err) {
      console.log(err);
    });
  }
  //滚动条
  $(".buildWorkRangeList_scroll").yi_scroll();
  $(".buildWorkRangeItem_scroll").yi_scroll();


  //工作范围管理  关闭窗口
  $('.buildWorkRange-pop').on('click', function (event) {
    if ($(event.target).is('.cd-popup-close1') || $(event.target).is('.cd-popup-close') || $(event.target).is('.buildWorkRange-pop')) {
      event.preventDefault();
      $(this).removeClass('is-visible3');
      buildWorkRangeArr = [];
      // buildWorkRangeArr = [];
      $('.buildWorkRangeItem ul').html('');

      $('.buildWorkRange-trigger1').val('');
      $('.buildWorkRange-trigger').val('');
    }
  });
  // 工作范围选定的数据
  // var buildWorkRangeArr = [];

  // 工作范围模糊查询
  buildWorkRangeFunc();

  function buildWorkRangeFunc() {
    var timeoutflag = null;
    //模糊查询
    $('body').on('click', function () {
      $("#buildWorkRangeModal .searchList").hide();
    });
    $("#buildWorkRangeModal .buildWorkRangeSearch").on("input", function (e) {
      $("#buildWorkRangeModal .searchList").show();

      if ($("#buildWorkRangeModal .searchList li")) { //键盘抬起后就判断页面中是否存在这个元素,存在就删除
        $("#buildWorkRangeModal .searchList li").remove();
      }
      var buildWorkRangeContent = $("#buildWorkRangeModal .buildWorkRangeSearch").val();
      buildWorkRangeContent = $.trim(buildWorkRangeContent);
      console.log(buildWorkRangeContent)

      // $.ajax({
      //   url: "/building/getBuildings?address=" + buildWorkRangeContent + "&size=10&page=0",
      //   type: "GET",
      //   async: false,
      //   success: function (result) {
      //     console.log(result)
      //     var data = result.data.content;
      //     for (var i = 0; i < data.length; i++) {

      //       $("#buildWorkRangeModal .searchList").append('<li>' + data[i].address + '</li>');


      //     }
      //   }
      // });

      var keyWord = $('.buildWorkRangeSearch').val();
      keyWord.trim();
      console.log(keyWord)
      var len = buildWorkRangeContent.length;
      var arr = [];
      $("#buildWorkRangeModal .searchList").html('');
      if ($('.buildWorkRangeSearch').val()) {
        for (var i = 0; i < len; i++) {
          //如果字符串中不包含目标字符会返回-1
          if (buildWorkRangeContent[i].address) {
            if (buildWorkRangeContent[i].address.indexOf(keyWord) >= 0) {
              arr.push(buildWorkRangeContent[i]);
              $('#buildWorkRangeModal .searchList').append('<li>' + buildWorkRangeContent[i].address + '</li>');
              console.log(buildWorkRangeContent[i].address)
              // $('#buildWorkRangeModal .tbodyList .build'+buildWorkRangeContent[i].id).show();
            } else {
              // $("#buildWorkRangeModal .searchList").html('');
              // $('#buildWorkRangeModal .tbodyList .build'+buildWorkRangeContent[i].id).hide();
            }
          }

        }
      }


      $("#buildWorkRangeModal .searchList").show();

    });
    //直接输入地址,点击列表查询
    $("#buildWorkRangeModal .searchList").on("click", "li", function () {
      $("#buildWorkRangeModal .searchList").show();
      var liVal = $(this).text();
      liVal = $.trim(liVal);
      var searchVal = $("#buildWorkRangeModal .buildWorkRangeSearch").val(liVal);
      buildWorkRangeContent = liVal;
      $("#buildWorkRangeModal .searchList").html(""); //hide
      return false;
    });
  }

  //    搜索工作范围
  $('#buildWorkRangeSearchBtn').on('click', function () {
    // var buildWorkRangeSearchVal = $('.buildWorkRange-content .buildWorkRangeSearch').val();
    // runderBuildWorkRangeList();

    //字符串方法indexOf
    var keyWord = $('.buildWorkRangeSearch').val();
    console.log(keyWord)
    var len = buildWorkRangeContent.length;
    var arr = [];
    if (!$('.buildWorkRangeSearch').val()) {

    }
    for (var i = 0; i < len; i++) {
      //如果字符串中不包含目标字符会返回-1
      if (buildWorkRangeContent[i].address) {
        if (buildWorkRangeContent[i].address.indexOf(keyWord) >= 0) {
          arr.push(buildWorkRangeContent[i]);
          $('#buildWorkRangeModal .tbodyList .build' + buildWorkRangeContent[i].id).show();
        } else {
          $('#buildWorkRangeModal .tbodyList .build' + buildWorkRangeContent[i].id).hide();
        }
      }

    }
    console.log(arr, buildWorkRangeContent)
  });

  // 勾选楼宇
  $('#buildWorkRangeModal table').on('change', 'input', function () {
    var index = $(this).parent().parent().index();
    var that = this;
    $('.buildWorkRangeItem ul').html('');
    var num = 1;
    buildWorkRangeArr = [];
    $('#buildWorkRangeModal table input[type=checkbox]:checked').each(function (i, item) {
      num = i;
      var index = $(this).parent().parent().index();
      buildWorkRangeArr.push(buildWorkRangeContent[index]);
      $('.buildWorkRangeItem ul').append('<li>' + (i + 1) + '、' + buildWorkRangeContent[index].address + '</li>'); //buildWorkRangeContent[i].address
    });

    // if(this.checked){
    //   buildWorkRangeArr.push(buildWorkRangeContent[index]);
    //   for(var i=0;i<buildWorkRangeArr.length;i++){
    //     $('.buildWorkRangeItem ul').append('<li class="num'+index+'">'+(i+1)+'、'+buildWorkRangeContent[i].address+'</li>');
    //   }
    // }else{
    //   // console.log($('.buildWorkRangeItem ul num'+index))
    //   // $('.buildWorkRangeItem ul .num'+index).remove();     
    //   buildWorkRangeArr.splice(num,1);
    //   for(var i=0;i<buildWorkRangeArr.length;i++){
    //     $('.buildWorkRangeItem ul').append('<li class="num'+index+'">'+(i+1)+'、'+buildWorkRangeContent[i].address+'</li>');
    //   }
    // }
    console.log(buildWorkRangeArr)
  });
  // 点击工作范围确定按钮
  $('.buildWorkRangeSubmit').on('click', function () {
    console.log(buildWorkRangeArr)
    // 工作范围选定的数据
    // buildWorkRangeArr = buildWorkRangeArr;
    $('.buildWorkRange-pop').removeClass('is-visible3');
    var buildWorkRangeStr = '';
    console.log(buildWorkRangeArr.length);
    if (buildWorkRangeArr.length > 1) {
      for (var i = 0; i < buildWorkRangeArr.length; i++) {
        buildWorkRangeStr = buildWorkRangeStr + buildWorkRangeArr[i].address + '、';
      }
    } else if (buildWorkRangeArr.length == 1) {
      for (var i = 0; i < buildWorkRangeArr.length; i++) {
        buildWorkRangeStr = buildWorkRangeStr + buildWorkRangeArr[i].address;
      }
    }
    var buildWorkRangeArr0 = [];
    for (var i = 0; i < buildWorkRangeArr.length; i++) {
      buildWorkRangeArr0.push('{"id":' + buildWorkRangeArr[i].id);
    }
    var buildWorkRangeArrStr = JSON.stringify(buildWorkRangeArr0);
    if(buildWorkRangeStr.length>9){
      buildWorkRangeStr=buildWorkRangeStr.slice(0, 9)+'...';
    }else if(buildWorkRangeStr.length<1){
      buildWorkRangeStr = '';
    }
    
    $('.buildWorkRange-trigger1').val(buildWorkRangeArrStr);
    $('.buildWorkRange-trigger').val(buildWorkRangeStr);

  });



});